//
//  TopicHeadMoreViewController.m
//  ArtDog
//
//  Created by dllo on 16/9/9.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "TopicHeadMoreViewController.h"
#import "BaseCollectionView.h"
#import "HLMacro.h"
#import "UIColor+Common.h"
#import "ReadCollectionViewCell.h"
#import "ZWYNetTool.h"
#import "ReadMainModel.h"
#import "MJRefresh.h"
#import "MJRefreshComponent+RefreshStyle.h"
#import "NSString+Common.h"
#import "HalfImageAndWordsCollectionCell.h"
#import "DetailsWebViewController.h"

@interface TopicHeadMoreViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) BaseCollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray *loadArray;

@property (nonatomic, assign) NSInteger pageIndex;

@end

@implementation TopicHeadMoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
    [self setupNavigationBar];
    [self createCollectionView];
    [self refreshCollectionView];
}

- (void)setupNavigationBar
{
    self.navigationItem.title = @"悦读";
    
}

- (void)createCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    _collectionView = [[BaseCollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) collectionViewLayout:flowLayout];
    [self.view addSubview:_collectionView];
    flowLayout.minimumLineSpacing = 5;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_WIDTH / 3.6);
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    [_collectionView registerNib:[UINib nibWithNibName:@"HalfImageAndWordsCollectionCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"halfImageAndWordsCollectionCell"];
}

#pragma mark - 数据请求
- (void)handleData
{
    _pageIndex = 1;
    NSString *urlString = [self getUrlString];
    [ZWYNetTool GET:urlString andBody:nil andHeader:HOSTHEADER andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _loadArray = [NSMutableArray array];
        for (NSDictionary *dic in result[@"data"]) {
            ReadMainModel *model = [ReadMainModel modelWithDic:dic];
            [_loadArray addObject:model];
        }
        [_collectionView.mj_header endRefreshing];
        [_collectionView reloadData];
    } andFailureBlock:^(NSError *error) {
        [_collectionView.mj_header endRefreshing];
        NSLog(@"%@", error);
    }];
}

- (void)handleLoadData
{
    NSString *urlString = [self getUrlString];
    [ZWYNetTool GET:urlString andBody:nil andHeader:HOSTHEADER andResponse:ZWYJSON andSuccessBlock:^(id result) {
        for (NSDictionary *dic in result[@"data"]) {
            ReadMainModel *model = [ReadMainModel modelWithDic:dic];
            [_loadArray addObject:model];
        }
        [_collectionView.mj_footer endRefreshing];
        [_collectionView reloadData];
    } andFailureBlock:^(NSError *error) {
        [_collectionView.mj_footer endRefreshing];
        NSLog(@"%@", error);
    }];
}

- (NSString *)getUrlString
{
    NSString *urlString;
    
    urlString = [NSString getURLStringWith:HOST andPaths:@[@"artgoer/api/v1/user", [NSString getUserDefaultId], @"topic/readMore"] andParams:@{@"pageIndex": [NSString stringWithFormat:@"%ld", _pageIndex], @"token": [NSString getUserDefaultToken]}];
    
    return urlString;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _loadArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HalfImageAndWordsCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"halfImageAndWordsCollectionCell" forIndexPath:indexPath];
    cell.readModel = _loadArray[indexPath.item];
    
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DetailsWebViewController *detailsVC = [[DetailsWebViewController alloc]init];
    
    detailsVC.urlString = [NSString getURLStringWith:HOST andPaths:@[@"artgoer/api/v1/user", [NSString getUserDefaultId], @"discovery/v3/selectTopicDetailInfo"] andParams:@{@"scourceTable": @"topic", @"token": @"df68e038-143e-41cb-b554-456f78f184fc", @"clientType": @"1", @"topicId": [NSString stringWithFormat:@"%@", [_loadArray[indexPath.item] idField]], @"topicType": @"4"}];
    [self.navigationController pushViewController:detailsVC animated:YES];
}

#pragma mark - 刷新
- (void)refreshCollectionView
{
    _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self handleData];
    }];
    [_collectionView.mj_header mjRefreshStateHeaderStyle];
    
    _collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        if (_loadArray.count % 10 == 0) {
            _pageIndex = _loadArray.count / 10 + 1;
        } else {
            _pageIndex = _loadArray.count / 10 + 2;
        }
        [self handleLoadData];
    }];
    [_collectionView.mj_footer mjRefreshAutoStateFooterStyle];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
